<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 优惠券ajax类
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 */

//引用优惠券类
quanli_require('module/base/promotion/quanliCoupon.class.php');

class ajax_coupon{

    public function execute() {

        if (isset($_REQUEST['operFlg'])) {

            switch ($_REQUEST['operFlg']) {

                case 1:     //优惠券列表信息
                    $this->couponList();
                    break;

                case 2:     //保存优惠券信息
                    $this->editCoupon();
                    break;

                case 3:     //变更使用状态
                    $this->changeStatus();
                    break;

                case 4: //查看优惠券编号
                    $this->couponCodeList();
                    break;

                case 5: //生成编号优惠券
                    $this->createCode();
                    break;

                case 7:  //发放账户礼品券
                    $this->issueAccountCoupon();
                    break;
                //add by 2014-10-29 yim.hu FUN-BK-0010 重置优惠券列表信息 start
                case 8://重置优惠券列表信息
                	$this->resetCoupon();
                	break;
               
                case 9://重置优惠券信息
                	$this->resetCoupon_info();
                	break;
                //add by 2014-10-29 yim.hu FUN-BK-0010 重置优惠券列表信息 end
                default:
                    $this->toError('参数错误');
                    break;
            }

        } else {

            $this->toError('参数错误');

        }

    }

    /**
     * 返回错误信息
     * @param string $message 错误提示信息
     * @param int $code 错误代码
     */
    private function toError( $message, $code = 400 ) {

        die ( json_encode( array( 'code' => $code, 'message' => $message ) ) );

    }

    /*
     * 优惠券列表信息
     */
    private function couponList() {

        global $G_PGLOB;

        //优惠券类
        $quanliCoupon = new quanliCoupon();

        //转义优惠券名称中的%
        if(isset($_REQUEST['couponName'])){
            $_REQUEST['couponName'] = str_replace('%', '\%', $_REQUEST['couponName']);
        }

        //具体优惠券编号
        if(isset($_REQUEST['code']) && !empty($_REQUEST['code'])){
            $extend = 'AND couponID = (SELECT couponID FROM promotion_coupon_code WHERE code = "'.$_REQUEST['code'].'") ';
            $_REQUEST['extend'] = $extend;
        }

        if(isset($_REQUEST['couponID']) && !empty($_REQUEST['couponID'])){
            $_REQUEST['couponID'] = intval($_REQUEST['couponID']);
        }

        //查询
        $result_list = $quanliCoupon->searchCoupon( $_REQUEST );

        //返回数组
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($result_list){

            $city_array = $G_PGLOB->getCache('city');
            $city_map = array();
            foreach($city_array as $item)
            {
                $city_map[$item['id']] = $item['name'];
            }

            foreach ($result_list as $coupon){
                //查询优惠券对应城市
                $arg_array = array(
                    'couponID' => $coupon['couponID'],
                    'sortname' => 'cityID',
                    'sortorder' => 'ASC'
                );
                $coupon_city = $quanliCoupon->searchCouponCity( $arg_array );

                $cityName_str = ''; //城市 多个城市用逗号分隔
                if( $coupon_city ){

                    foreach( $coupon_city as $v){
                        $cityName_str .= $city_map[$v['cityID']] . ",";
                    }

                    $cityName_str = substr($cityName_str,0,-1);
                }

                //优惠券类型 & 操作
                if ($coupon['couponType'] == 1) {
                    if ($coupon['isGenerate'] == 0) {
                        $action = '<a href="javascript:void(0);" onclick="coupon.edit(' . $coupon['couponID'] . ')">修改</a>&nbsp;';
                        $action .= '<a href="javascript:void(0);" onclick="coupon.create.execute(' . $coupon['couponID'] . ')" target="_blank">生成优惠券编号</a>&nbsp;';
                    } else {
                        //mod by dongleixin 修改跳转新页面为在本页面打开 start
                        //$action = '<a href="/quanliadmin/app/promotion/coupon.pscript?operFlg=4&id=' . $coupon['couponID'] . '" target="_blank">查看优惠券编号</a>&nbsp;';
                        $action = '<a href="/quanliadmin/app/promotion/coupon.pscript?operFlg=4&id=' . $coupon['couponID'] . '">查看优惠券编号</a>&nbsp;';
                        //mod by dongleixin 修改跳转新页面为在本页面打开 end
                        $action .= '<a href="javascript:void(0);" onclick="coupon.exportCoupon(' . $coupon['couponID'] . ',' . $coupon['totalNum'] . ')">导出</a>';
                    }
                } else if ($coupon['couponType'] == 2) {
                    $action = '<a href="javascript:void(0);" onclick="coupon.edit(' . $coupon['couponID'] . ')">修改</a>&nbsp;';
                    $action .= '<a href="/quanliadmin/app/promotion/coupon.pscript?operFlg=4&id=' . $coupon['couponID'] . '">查看优惠券编号</a>&nbsp;';
                    $action .= '<a href="javascript:void(0);" onclick="coupon.grant(' . $coupon['couponID'] . ')">发放</a>&nbsp;';
                    $action .= '<a href="javascript:void(0);" onclick="coupon.import(' . $coupon['couponID'] . ')">导入</a>&nbsp;';
                    $action .= '<a href="javascript:void(0);" onclick="coupon.bigImport(' . $coupon['couponID'] . ')">大批量导入</a>&nbsp;';
                } else {
                    $action = '<a href="javascript:void(0);" onclick="coupon.edit(' . $coupon['couponID'] . ')">修改</a>&nbsp;';
                }

                //使用状态
                if ($coupon['useFlg'] == 1) {
                    $status = "启用";
                } else {
                    $status = "禁用";
                }

                $result_array['rows'][] = array(
                    'id' => $coupon['couponID'],
                    'cell' => array(
                        $action,
                        '<span id="useFlg_' . $coupon['couponID'] . '" ><a href="javascript:void(0);" onclick="coupon.status.change(' . $coupon['couponID'] . ',' . $coupon['useFlg'] . ');">' . $status . '</a></span>',
                        $coupon['couponID'],
                        $coupon['couponName'],
                        $coupon['startDate'],
                        $coupon['endDate'],
                        $coupon['limitMoney'],
                        $coupon['amount'],
                        $coupon['type_name'],
                        $coupon['useTypeName'],
                        $cityName_str,
                        $coupon['wncode'],
                        $coupon['totalNum'],
                        $coupon['getNum'],
                        $coupon['useNum']
                    )
                );

            }

        }

        echo json_encode($result_array);

    }

    /**
     * 保存优惠券信息
     */
    private function editCoupon() {

        global $G_PGLOB;
        //$city_str = isset($_REQUEST['cityID']) ? implode(",",$_REQUEST['cityID']) : ""; //存储城市ID的字符串
        //error_log( var_export($_REQUEST['cityID'],true));
        //$_REQUEST['cityID'] = $city_str;
        //优惠券操作类
        $quanliCoupon = new quanliCoupon($G_PGLOB->DBCA);

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
        $result_array = $quanliCoupon->editCoupon( $_REQUEST );

        echo json_encode( $result_array );

    }

    /**
     * 变更使用状态
     */
    private function changeStatus () {

        if (!isset($_REQUEST['couponID']) || !is_numeric($_REQUEST['couponID']) ) {
            $this->toError('参数错误');
        }

        global $G_PGLOB;

        //实例化优惠券类
        $quanliCoupon = new quanliCoupon( $G_PGLOB->DBCA );

        //变更使用状态
        $result_array = $quanliCoupon->changeCouponStatus( $_REQUEST['couponID'] );

        $this->toError( $result_array['message'], $result_array['code'] );

    }

    /**
     * 查看优惠券
     */
    private function couponCodeList() {

        global $G_PGLOB;

        //实例化优惠券类
        $quanliCoupon = new quanliCoupon( $G_PGLOB->DBCA );

        //查询
        $coupon_code_array = $quanliCoupon->searchCouponCode( $_REQUEST );


        //返回数组
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($coupon_code_array){
            if( isset($coupon_code_array['code']) && !empty($coupon_code_array['code'])){
                $result_array['rows'][] = array(
                    'id' => $coupon_code_array['code'],
                    'cell' => array(
                        $coupon_code_array['code'],
                        $coupon_code_array['pcustID'],
                        $coupon_code_array['createTime'],
                        $coupon_code_array['endDate'],
                        $coupon_code_array['useTime'],
                        $coupon_code_array['bill_code'],
                        $coupon_code_array['useAmount']
                    )
                );
            }else{
                foreach ($coupon_code_array as  $coupon_code) {
                    $result_array['rows'][] = array(
                        'id' => $coupon_code['code'],
                        'cell' => array(
                            $coupon_code['code'],
                            $coupon_code['pcustID'],
                            $coupon_code['createTime'],
                            $coupon_code['endDate'],
                            $coupon_code['useTime'],
                            $coupon_code['bill_code'],
                            $coupon_code['useAmount']
                        )
                    );

                }
            }


        }

        echo json_encode( $result_array );


    }

    /**
     * 生成优惠券编号
     */
    private function createCode () {

        if(!isset($_REQUEST['couponID']) || !is_numeric($_REQUEST['couponID']) ){
            $this->toError('缺少参数');
        }

        global $G_PGLOB;

        //实例化优惠券类
        $quanliCoupon = new quanliCoupon( $G_PGLOB->DBCA );

        //生成优惠券编号信息
        //mod by allen.qiang 20150423 增加操作者信息 start
        //$result_array = $quanliCoupon->createCouponCode( $_REQUEST['couponID'] );
        $result_array = $quanliCoupon->createCouponCode( $_REQUEST['couponID'],null,true,$G_PGLOB->currentUserID);
        //mod by allen.qiang 20150423 增加操作者信息 end

        echo json_encode( $result_array );

    }

    /**
     * 发放账户礼品券
     */
    private function issueAccountCoupon() {

        if(!isset($_REQUEST['couponID']) || !is_numeric($_REQUEST['couponID']) ){
            $this->toError('参数错误');
        }

        if(!isset($_REQUEST['data']) || empty($_REQUEST['data']) || !is_array($_REQUEST['data']) ){
            $this->toError('没有选择会员', 400);
        }

        global $G_PGLOB;

        //实例化优惠券类
        $quanliCoupon = new quanliCoupon( $G_PGLOB->DBCA );

        //发放优惠券
        //add by allen.qiang 2015-04-08 增加操作者信息 start
        //$result_array = $quanliCoupon->createCouponCode($_REQUEST['couponID'], $_REQUEST['data']);
        $result_array = $quanliCoupon->createCouponCode($_REQUEST['couponID'], $_REQUEST['data'],true,$G_PGLOB->currentUserID);
        //add by allen.qiang 2015-04-08 增加操作者信息 end
        returnJsonData($result_array['message'],$result_array['code']);

    }
    //add by 2014-10-29 yim.hu FUN-BK-0010 查询优惠券编号以及变更状态 start    
    private function resetCoupon() {
    
        global $G_PGLOB;

        //实例化优惠券类
        $quanliCoupon = new quanliCoupon( $G_PGLOB->DBCA );
	
        //查询
        $coupon_code_array = $quanliCoupon->searchCouponCode( $_REQUEST );

		//这里开始判断是否需要重置优惠券
		if($coupon_code_array['billNo']==''){
			$status ='未使用';
			$tishi_name ='';
			
		}else{
			$status ='已使用';
			$code = $coupon_code_array['code'];
			$tishi_name="<a  onClick=reset_yhj('".$code."')>重置优惠券</a>";
		}
		
		
        //返回数组
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

//        if ($coupon_code_array){
            if( isset($coupon_code_array['code']) && !empty($coupon_code_array['code'])){
                $result_array['rows'][] = array(
                    'id' => $coupon_code_array['code'],
                    'cell' => array(
                    	$tishi_name,
                        $coupon_code_array['code'],
                        $coupon_code_array['pcustID'],
                        $coupon_code_array['createTime'],
                        $coupon_code_array['endDate'],
                        $coupon_code_array['useTime'],
                        $coupon_code_array['bill_code'],
                        $coupon_code_array['useAmount'],
                    	$coupon_code_array['amount'],
                    	$status		
                    )
                );
        }

        echo json_encode( $result_array );
    
    }
   
    //变更优惠券状态
    public function resetCoupon_info(){
    	


        global $G_PGLOB;

        //实例化优惠券类
        $quanliCoupon = new quanliCoupon( $G_PGLOB->DBCA );

        //变更使用状态
        $result_array = $quanliCoupon->resetCouponStatus( $_REQUEST['code'] );
		echo $result_array['code'];
 
    	
    }
    //add by 2014-10-29 yim.hu FUN-BK-0010 查询优惠券编号以及变更状态 end
}